#ifndef __SERVICE_H__
#define __SERVICE_H__

#include "TaskQueue.h"
#include <thread>
#include <vector>

class Service
{
public:
	Service() {};
	~Service() { Shutdown(); }

public:
	void Init();
	int GetWokerCount() const { return wokers_.size(); }

public:
	void AddTask(Task* task);
	void AddTaskFront(Task* task);
	void AddCompleteTask(Task* task);
	bool CancelById(unsigned int id);
	void CancelByOwner(void* owner);

	void Tick(float dt);
	void Shutdown();

	static int GetCurWorkerIndex() { return curWorkerIndex_; }

	void PerformInMainThread(const std::function<void()>& func);
	void PerformInNextLoop(const std::function<void()>& func);

	void AddTickFunction(const std::function<void(float)>& func);

	void PerformAsyn(const std::function<void()>& fun);
private:
	void Worker(int index);
	Task* GetNextTask();

private:
	TaskQueue queue_;
	TaskQueue completeQueue_;
	std::vector<std::thread> wokers_;
	std::vector<std::function<void(float)>> tickFuncs_;
	std::vector<std::function<void()>> nextFuncs_;

	static thread_local int curWorkerIndex_;
};



#endif // __SERVICE_H__
